modname = "AdminWatch"
version = "0.53"
min_level     = 16
adminchatlevel = 15
et.CS_PLAYERS = 689
admin_cmd 			= "!ad"
cancel_cmd 			= "!cancel"
pass_cmd 			= "!pass"
start_cmd 			= "!start"
help_cmd 			= "!he"
list_cmd 			= "!listplayers"
put_cmd 			= "!putteam"
next_cmd 			= "!nextmap"
set_cmd 			= "!setlevel"
ref_cmd				= "!ref"
revive_cmd				= "!rev"

----------------------------------------------------------------------------------------------
canspec = {}
maxclients       = 0

function et_InitGame(levelTime, randomSeed, restart)
  et.RegisterModname(modname .. " " .. version)
  gamestate = tonumber(et.trap_Cvar_Get( "gamestate" ))
  
  maxclients = tonumber(et.trap_Cvar_Get("sv_maxclients"))
  for i = 0, maxclients - 1, 1 do
	canspec[i] = 0
	  if gamestate == 3 then
		changeclassback(i)
	  end
  end
end


function et_ClientDisconnect(id) -- client disconnects
  canspec[id] = 0
end

local helpa_cmd
helpa_cmd = "!helpa"

function et_ClientCommand(clientNum, command) -- get client commands
	local arg0 = string.lower(et.trap_Argv(0))
    local arg1 = et.trap_Argv(1)
    if arg0 == "say" then
		if arg1 == helpa_cmd then
			if getlevel(clientnum) then
			et.trap_SendServerCommand( clientNum, "cpm \"^1Commands^3:\n\ ^7/gib name  ^1-^8kills someone\n\ ^7/get name   ^1-^8teleports someone to you\n\"" )
			et.trap_SendServerCommand( clientNum, "cpm \" ^7/goto name  ^1-^8teleports you to someone\n\"" )
			et.trap_SendServerCommand( clientNum, "cpm \" ^7/port x x x  ^1-^8teleports you to a value\n\"" )
			et.trap_SendServerCommand( clientNum, "cpm \" ^7/ma  ^1-^8adminchat\n\ ^7!ref  ^1-^8referee login\n\"" )
			et.trap_SendServerCommand( clientNum, "cpm \" ^7/antigib 1/0  ^1-^8cycles the antigib\n\ ^7/sc ^1-^8shoutcaster\n\"" )
			et.trap_SendServerCommand( clientNum, "cpm \" ^7/thirdperson 1/0  ^1-^8cycles the thirdperson (it's limited)\n\"" )
			et.trap_SendServerCommand( clientNum, "cpm \" ^7/hide 1/0 ID/name  ^1-^8fake covert ops\n\"" )
			et.trap_SendServerCommand( clientNum, "cpm \" ^7/hardcore 1/0  ^1-^8cycles the hardcore mode\n\"" )
			return 1
		end
	end
	end
local cmd = string.lower(command)
	if cmd == "sc" then
	  if et.G_shrubbot_level(clientNum) >= 9 then
	  canspec[clientNum] = 1 
	  et.trap_SendServerCommand(clientNum, "cpm \"^3You are able to spec now!!\n\" " )
	  return 1
	 end
	  et.trap_SendServerCommand(clientNum, "cpm \"^3Sorry, ur not an Admin\n\" " )
	  return 1
	end
  -- check if client is in list of allowed admins
   if et.G_shrubbot_level(clientNum) >= adminchatlevel then
    if cmd == "ma" then
      if et.trap_Argc() > 1 then
        -- build message
        local message = ""
        for i = 1, et.trap_Argc() - 1, 1 do
          message = message .. et.trap_Argv(i) .. " "
        end
        -- send message to all other admins
        for i = 0, maxclients - 1, 1 do
          	  if et.G_shrubbot_level(i) >= adminchatlevel then
            sendstring = playerName(clientNum) .. " ^w(^8adminchat^w)^8: " .. message
            et.trap_SendServerCommand(i, "chat \"" .. sendstring .. "\"")
          end
        end
      end
      return 1
    end
  end
  --[[if cmd == "ref" then
  	local isref = et.Info_ValueForKey( et.trap_GetConfigstring( et.CS_PLAYERS + clientNum ), "ref" )
	if getlevel(clientNum) and isref == "0" then
		et.gentity_set(clientNum, "sess.referee", 1)
		et.trap_SendConsoleCommand( et.EXEC_APPEND, "ref referee " .. clientNum .. " \n" )
		et.Info_SetValueForKey( et.trap_GetConfigstring( et.CS_PLAYERS + clientNum ), "ref", "1" )
		et.trap_SendServerCommand(clientNum, "cp \"^3*** You have gained referee status ***\n\" " )
		et.G_Sound(clientNum, et.G_SoundIndex("/sound/misc/referee.wav"))
		return 1
	end
	if not getlevel(clientNum) then
		et.trap_SendServerCommand(clientNum, "cpm \"^3Sorry, ur not an Admin\n\" " )
		return 1
	end
  --if cmd == "ref logout" and isref == "1" then
		et.gentity_set(clientNum, "sess.referee", 0)
		et.trap_SendConsoleCommand( et.EXEC_APPEND, "ref referee " .. clientNum .. " \n" )
		et.Info_SetValueForKey( et.trap_GetConfigstring( et.CS_PLAYERS + clientNum ), "ref", "0" )
		et.trap_SendServerCommand(clientNum, "cp \"^3*** You have been stripped referee status ***\n\" " )
		et.G_Sound(clientNum, et.G_SoundIndex("/sound/misc/referee.wav"))
  end--]]
  if cmd == "get" then
  if getlevel(clientNum) then
	local team = checkteam(clientNum)
		if team == 3 then
			pos = et.gentity_get(clientNum, "s.origin")
		elseif team == 1 or team == 2 then
			pos = et.gentity_get(clientNum, "origin")
		end
		if getlevel(clientNum) then
		  	local getplayer = getPlayerId(et.trap_Argv(1))
			et.gentity_set(getplayer, "origin", pos)
			et.trap_SendServerCommand(clientNum, "cpm \"^7"..playerName(getplayer).." ^3has been moved to ur position!\n\" " )
		return 1
		end
		end
	end
	if cmd == "goto" then
	if getlevel(clientNum) then
		local getplayer1 = getPlayerId(et.trap_Argv(1))
		local team = checkteam(getplayer1)
			if team == 3 then
				gpos = et.gentity_get(getplayer1, "s.origin")
			elseif team == 1 or team == 2 then
				gpos = et.gentity_get(getplayer1, "origin")
			end
			et.gentity_set(clientNum, "origin", gpos)
			et.trap_SendServerCommand(clientNum, "cpm \"^3You have been moved to ^7"..playerName(getplayer1).." ^3position!\n\" " )
			return 1
			end
	end
	if getlevel(clientNum) then
		if cmd == "port" then
			if et.trap_Argv(1) then
				if et.trap_Argv(2) then
					if et.trap_Argv(3) then
						local portnum = ""..et.trap_Argv(1).." "..et.trap_Argv(2).." "..et.trap_Argv(3)..""
						et.gentity_set(clientNum, "origin", portnum)
						et.trap_SendServerCommand(clientNum, "cpm \"^3You have been moved to position ^7"..portnum.." ^3!\n\" " )
						return 1
					end
				end
			end
		end
	end
  if cmd == "gib" then
	 if getlevel(clientNum) then
        gotohell(getPlayerId(et.trap_Argv(1)))
	return 1
		end
	end
  if cmd == "revive" then
		if getlevel(clientNum) then
			revive(getPlayerId(et.trap_Argv(1)), clientNum)
			return 1
		end
  end
  if cmd == "war" then
	 if getlevel(clientNum) then
        et.trap_SendConsoleCommand(et.EXEC_APPEND, "exec \"server2.cfg\n\" ")
	return 1
		end
	end
  if cmd == "pub" then
	 if getlevel(clientNum) then
        et.trap_SendConsoleCommand(et.EXEC_APPEND, "exec \"server.cfg\n\" ")      
	return 1
		end
	end
    if cmd == "m" then
          if getlevel(clientNum) then
			return 0
          end
			if not getlevel(clientNum) then
				et.trap_SendServerCommand(clientNum, "cpm \"^3Sorry, you can't use ^1Private Chat\n\" " )
				return 1
			end
    end
	if et.trap_Argv(0) == "say" or et.trap_Argv(0) == "say_team" or et.trap_Argv(0) == "say_buddy" or et.trap_Argv(0) == "say_teamnl" then
		if string.find(et.trap_Argv(1), "^" .. cancel_cmd .. "") and not string.find(et.trap_Argv(1), "^!cancelvote") then
			et.trap_SendConsoleCommand( et.EXEC_APPEND, "cancelvote" )
		elseif string.find(et.trap_Argv(1), "^" .. pass_cmd .. "") and getlevel(clientNum) and not string.find(et.trap_Argv(1), "^!passvote") then
			et.trap_SendConsoleCommand( et.EXEC_APPEND, "passvote" )
		elseif string.find(et.trap_Argv(1), "^" .. start_cmd .. "") and getlevel(clientNum) then
			et.trap_SendConsoleCommand( et.EXEC_APPEND, "start_match\n" )
		elseif string.find(et.trap_Argv(1), "^" .. admin_cmd .. "") and not string.find(et.trap_Argv(1), "^!admintest") then
			et.trap_SendConsoleCommand( et.EXEC_APPEND, "admintest ".. clientNum.."\n" )
		elseif string.find(et.trap_Argv(1), "^" .. help_cmd .. "") and not string.find(et.trap_Argv(1), "^!help") then
			et.trap_SendConsoleCommand( et.EXEC_APPEND, "help" )
		elseif string.find(et.trap_Argv(1), "^" .. list_cmd .. "") then --and not string.find(et.trap_Argv(1), "^!list") then
			et.trap_SendServerCommand(clientNum, "cp \"^wUsage^z: ^1!list\"" )
		elseif string.find(et.trap_Argv(1), "^" .. next_cmd .. "") then
			et.trap_SendServerCommand(clientNum, "cp \"^wUsage^z: ^1!next\"" )
		elseif string.find(et.trap_Argv(1), "^" .. set_cmd .. "") then
			et.trap_SendServerCommand(clientNum, "cp \"^wUsage^z: ^1!set\"" )
		elseif string.find(et.trap_Argv(1), "^" .. put_cmd .. "") then
			et.trap_SendServerCommand(clientNum, "cp \"^wUsage^z: ^1!put\"" )
		elseif string.find(et.trap_Argv(1), "^" .. revive_cmd .. "") and not string.find(et.trap_Argv(1), "^!revive") then
			et.trap_SendConsoleCommand( et.EXEC_APPEND, "revive "..et.trap_Argv(2).."" )
		elseif string.find(et.trap_Argv(1), "^" .. ref_cmd .. "") and getlevel(clientNum) then
			et.gentity_set(clientNum, "sess.referee", 1)
			et.Info_SetValueForKey( et.trap_GetConfigstring( et.CS_PLAYERS + clientNum ), "ref", 1 )
			et.G_Sound(-1, et.G_SoundIndex("/sound/misc/referee.wav"))
			et.trap_SendServerCommand(clientNum, "cpm \"^3*** You have gained referee status ***\n\" " )
			et.trap_SendServerCommand(-1, "cp \"^3"..playerName(clientNum).." ^7has gained referee status\n\" " )
			et.trap_SendConsoleCommand(et.EXEC_APPEND, "ref referee "..clientNum.."" )
		end
	end
	if cmd == "hide" then
		if not getlevel(clientNum) then
			et.trap_SendServerCommand(clientNum, "cpm \"^3Sorry, ur not an Admin\n\" " )
			return 1
		end
		local playerNum = getPlayerId(et.trap_Argv(2))
		if et.trap_Argv(1) == "1" then
			if getlevel(clientNum) then
			changeclass(playerNum)
			et.trap_SendServerCommand(playerNum, "chat \"^1Console: ^3Fake Covert Ops has been enabled\"" )
			if playerNum ~= clientNum then 	--prevents double message to yourself
				et.trap_SendServerCommand(clientNum, "chat \"^1Console: ^3Fake Covert Ops has been enabled for ^7"..playerName(playerNum).."\"" )
			end
			return 1
			end
		elseif et.trap_Argv(1) == "0" then
			if getlevel(clientNum) then
			changeclassback(playerNum)
			et.trap_SendServerCommand(playerNum, "chat \"^1Console: ^3Fake Covert Ops has been disabled\"" )
			if playerNum ~= clientNum then 	--prevents double message to yourself
				et.trap_SendServerCommand(clientNum, "chat \"^1Console: ^3Fake Covert Ops has been disabled for ^7"..playerName(playerNum).."\"" )
			end
			return 1
			end
		end
			et.trap_SendServerCommand(clientNum, "cpm \"^3/hide ^11/0\" " )
			return 1
	end
	return 0
end

function changeclass(client)
   local cs = et.trap_GetConfigstring(et.CS_PLAYERS + client)
	local infoclass = et.Info_SetValueForKey( cs, "c", 4 )
	et.trap_SetConfigstring(689 + client, infoclass)
	et.gentity_set(client, "ps.powerups", 8, 11 )
end

function changeclassback(client)
   local cs = et.trap_GetConfigstring(et.CS_PLAYERS + client)
	local infoclass = et.Info_SetValueForKey( cs, "c", 1 )
	et.trap_SetConfigstring(689 + client, infoclass)
	et.gentity_set(client, "ps.powerups", 8, 0 )
end

function revive(targetID, clientNum)
    -- test parameters
    if not targetID then
		et.trap_SendServerCommand(clientNum, "cpm \"^wMissing ID\n\" " )
		return 
    end
	for j = 0, maxclients - 1 do
		if checkteam(j) == 2 or checkteam(j) == 1 then
		local getplayer = getPlayerId(et.trap_Argv(1))
		local body = tonumber(et.gentity_get(targetID, "r.contents"))
			if body == 67108864 then
				et.gentity_set(targetID, "health", 123)
				et.gentity_set(targetID, "r.contents", 33554432) 
				soundindex = et.G_SoundIndex("sound/player/gasp.wav" )
				et.G_Sound(targetID, soundindex )
				et.trap_SendServerCommand(-1, "cpm \"^w"..playerName(getplayer).." ^3has been revived!\n\" " )
				--et.trap_SendConsoleCommand( et.EXEC_APPEND, "revive "..targetID.."\n" )
				return 1
			end
			if body == 33554432 then
				et.trap_SendServerCommand(clientNum, "cpm \"^w"..playerName(getplayer).." ^3must be dead before you can revive him!\n\" " )
				return 1
			end
		end
	end
end


function et_RunFrame( levelTime )
	for i = 0, (maxclients - 1) do
		if et.G_shrubbot_level(i) >= 9 and canspec[i] == 1 then
		et.gentity_set(i,"sess.spec_invite",3)
	 end
  end
end

function et_ClientDisconnect(clientNum)
	local isref = et.Info_ValueForKey( et.trap_GetConfigstring( et.CS_PLAYERS + clientNum ), "ref" )
	et.Info_SetValueForKey( et.trap_GetConfigstring( et.CS_PLAYERS + clientNum ), "ref", "0" )
end

-- gets user's guid
-- returns nil if not applicable to entity number
function getguid(targetID)
    if (targetID == nil) or (targetID > maxclients) then
      return nil
    end

    local userinfo = et.trap_GetUserinfo( targetID )
    local guid = et.Info_ValueForKey( userinfo, "cl_guid" )
    -- upcase for exact matches
    guid = string.upper(guid)

    return guid
end
function getlevel(client)
	local lvl = et.G_shrubbot_level(client)
	if lvl >= min_level then
		return true
	end
		return nil
end

function checkteam(client)
   local cs = et.trap_GetConfigstring(et.CS_PLAYERS + client)
	return tonumber(et.Info_ValueForKey(cs, "t"))
end

function setPosition(id, pos) -- set the player's position
  et.gentity_set(id, "origin", pos)
  return 1
end

function playerName(id) -- return a player's name
  local name = et.Info_ValueForKey(et.trap_GetUserinfo(id), "name")
  if name == "" then
    return "*unknown*"
  end
  return name
end

function gotohell(targetID)
    -- test parameters
    if not targetID then
        return
    end
for j = 0, maxclients - 1 do
	if checkteam(j) == 2 or checkteam(j) == 1 then
	local getplayer = getPlayerId(et.trap_Argv(1))
		et.gentity_set(targetID, "health", -200)
		soundindex = et.G_SoundIndex("sound/player/hurt_barbwire.wav" )
		et.G_Sound(targetID, soundindex )
		et.trap_SendServerCommand(-1, "cpm \"^w"..playerName(getplayer).." ^3has been gibbed!\n\" " )
		return 1
		end
	end
end

function printmsg(message, clientID)
    if not message then
        return
    end

    -- replace "s in message with 's
    local dummy
    message, dummy = string.gsub(message, "\"", "'")

    if clientID then
        et.trap_SendServerCommand(clientID, "print \"".. message .."^7\n\"")
    else
        et.G_Print(message .."^7\n")
    end
end

function getPlayerId(name, clientID)
    local i
    -- if it's nil, return nil and throw error
    if (name == "") then
        printmsg("Not enough parameters.", clientID)
        return nil
    end
    -- if it's a number, interpret as slot number
    local clientnum = tonumber(name)
    if clientnum then
        if (clientnum <= tonumber(et.trap_Cvar_Get("sv_maxclients"))) and et.gentity_get(clientnum,"inuse") then
            return clientnum
        else
            printmsg("Slot "..clientnum.." not in use.", clientID)
            return nil
        end
    end
    -- exact match first
    for i=0,et.trap_Cvar_Get("sv_maxclients"),1 do
      --playeri = et.gentity_get(i,"pers.netname")
        if playeri then
            if et.Q_CleanStr( playeri ) == et.Q_CleanStr( name ) then
                return i
            end
        end
    end
    -- partial match
    for i=0,et.trap_Cvar_Get("sv_maxclients"),1 do
       playeri = et.gentity_get(i,"pers.netname")
        if playeri then
            if (string.find(string.lower(et.Q_CleanStr( playeri )), string.lower(et.Q_CleanStr( name )), 1, true)) then
                return i
            end
        end
    end
    printmsg("No name match found for '"..name.."'.", clientID)
    return nil
end